package com.ellen.mac.chatdemo.adpter;

import android.content.Context;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.ellen.mac.chatdemo.R;
import com.ellen.mac.chatdemo.model.DataPhotoBean;
import com.squareup.picasso.Picasso;
import java.util.List;

/**
 * Created by j on 2017/12/15.
 */

public class NewImageAdapter extends BaseQuickAdapter<DataPhotoBean, BaseViewHolder> {
    private Context context;
    protected ImageView contentView;
    private static final int MAX_DEFAULT_HEIGHT = 400;
    private static final int MAX_DEFAULT_WIDTH = 300;

    public NewImageAdapter(int layoutResId, List data, Context context) {
        super(layoutResId, data);
        this.context = context;
    }

    @Override
    protected void convert(BaseViewHolder helper, DataPhotoBean item) {
        // 图片的真实高度与宽度
        double actualHight = item.getHeight();
        double actualWidth = item.getWidth();
        double viewHeight = MAX_DEFAULT_HEIGHT;
        double viewWidth = MAX_DEFAULT_WIDTH;
        if (0 != actualHight && 0 != actualWidth) {
            // 要保证图片的长宽比不变
            double ratio = actualHight / actualWidth;
            if (ratio > viewHeight / viewWidth) {
                viewHeight = (actualHight > viewHeight ? viewHeight : actualHight);
                viewWidth = viewHeight / ratio;
            } else {
                viewWidth = (actualWidth > viewWidth ? viewWidth : actualWidth);
                viewHeight = viewWidth * ratio;
            }
        }
        Picasso.with(context)
                .load(item.getUrl()).
                resize((int) viewWidth, (int) viewHeight)
                .centerCrop()
                .into((ImageView) helper.getView(R.id.chat_item_image_view));

        helper.addOnClickListener(R.id.chat_item_image_view);

    }
}